package com.ebelter.nb.ui.activitys;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.chipsea.healthscale.CsAlgoBuilderEx;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.R;
import com.ebelter.nb.ui.view.ScaleDataTipDialog;

import cn.jpush.android.api.JPushInterface;

public class CeshiActivity extends Activity {

    String TAG="CeshiActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ceshi);
    }

    public void AChick(View view) {
        CsAlgoBuilderEx mBuilderEx = new CsAlgoBuilderEx();

        int height=165;
        float weight2=100.1f;
        int sex=0;
        int orSex=1;
        int age=25;
        int resistance2=494;
        float fat2=34.8f;
        int profession=0;



        //实测阻抗
        float resistanceResult2= (float) ((resistance2*100+10000)/103-165);
        //加密阻抗
        resistanceResult2= (float) ((100.1*40+resistanceResult2*60+10000)*1.0/100.0);






        // 设置sdk基值
        mBuilderEx.setUserInfo(height, weight2
                , (byte) orSex, age, resistanceResult2);

        LogUtils.i(TAG, "用户  分析测量结果 要计算的信息----身高=" + height
                + "---体重 = " + weight2 + "----脂肪=" + fat2 + "----原阻抗 = " + resistance2 +"---解密阻抗（代入sdk里面的阻抗）="+resistanceResult2+
                "----性别（SDK 0女1男）=" + orSex +"---profession(1为运动员，0为普通用户) = "+profession+"---年龄="+age);

        //设置用户角色，1为运动员，0为普通用户
        mBuilderEx.setMode(profession);
        /*####################计算出各个指标值###############################*/
        float wr, bmr, vf, mv, sm, bv, protein, bmi;
        //BMI
        bmi = getBmi(weight2, height);

        int ba, score;
        if (fat2 > 0.0f && weight2 > 0 && (resistance2 > 0 && resistance2 < 65535)) {//脂肪率大于0则，计算各个指标
            wr = mBuilderEx.getTFR();
            bmr = mBuilderEx.getBMR();
            vf = mBuilderEx.getVFR();
            //骨骼肌
            mv = mBuilderEx.getSMM();
            sm = mBuilderEx.getSLM();
            bv = mBuilderEx.getMSW();
            ba = (int) mBuilderEx.getBodyAge();
            protein = mBuilderEx.getPM();
            score = mBuilderEx.getScore();
            // 蛋白质KG  转 百分比
            protein = dbzKgToPer(protein, weight2);
            LogUtils.i(TAG, "---- 水分 =" + wr + ", bmr =" + bmr + ", 内脂等级 =" + vf + ", 骨骼肌 =" + mv + ", 骨量 =" + bv+",肌肉量 ="+sm+", bmi ="+bmi
                    + ", 脂肪 = " + fat2 + ", 体重 = " + weight2 + ", 身体年龄 = " + ba + ", 蛋白质 = " + protein + ", 身体得分 = " + score);



        } else {//脂肪率小于等于0，则各个指标无效
            wr = bmr = vf = mv = sm = bv = protein = 0f;
            ba = score = 0;
        }
    }

    private float getBmi(float weight, float userHeight) {
        float bmi = weight / (userHeight * userHeight / 10000);
        return Math.round(bmi * 10) / 10.0f;
    }

    private float dbzKgToPer(float dbz, float weight) {
        float preF = dbz / weight * 100;
        preF = Math.round(preF * 10) / 10.0f;
        preF = Math.min(preF, 100.0f);
        return preF;
    }

    public void BClick(View view) {


        Intent intent=new Intent(JPushInterface.ACTION_MESSAGE_RECEIVED);
        Bundle bundle=new Bundle();
        String json="{\"deviceType\":1,\"deviceInfo\":{\"rssi\":10,\"unit\":0,\"usermode\":2,\"fat\":\"193\",\"weight\":\"678\",\"imei\":\"867726036757393\",\"resistor\":\"493\",\"hart\":\"90\",\"time\":\"2019-06-12 09:23:19\"}}";
        bundle.putString(JPushInterface.EXTRA_MESSAGE,json);
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }
}
